home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / powervww / pvcomlin.cpp < prev    next >
C/C++ Source or Header  |  1998-01-05  |  6KB  |  266 lines

  1. //  ____________________________________________________
  2. // |                                                    |
  3. // |  Project:     POWER VIEW INTERFACE                 |
  4. // |  File:        PVCOMLIN.CPP                         |
  5. // |  Compiler:    WPP386 (10.6)                        |
  6. // |                                                    |
  7. // |  Subject:     Command line processor               |
  8. // |                                                    |
  9. // |  Author:      Emil Dotchevski                      |
  10. // |____________________________________________________|
  11. //
  12. // E-mail: zajo@geocities.com
  13. // URL:    http://www.geocities.com/SiliconValley/Bay/3577
  14.  
  15. #ifndef NOPARAM
  16.  
  17. #define uses_ctype
  18. #define uses_dos
  19. #define uses_fcntl
  20. #define uses_malloc
  21. #define uses_process
  22. #define uses_stdio
  23. #define uses_stdlib
  24. #define uses_string
  25. #define uses_basics
  26. #define uses_comlin
  27. #define uses_system
  28.  
  29. #define DECLARE_PVCOMLIN
  30. #include "PVuses.h"
  31. #undef DECLARE_PVCOMLIN
  32.  
  33. #define PARAMS_BUFFER 0x1000
  34. #define ecUNKNOWN_PARAMS 222
  35. #define ecREAD_PARAMS 223
  36.  
  37. static char *params;
  38. static Tset used_params;
  39.  
  40. char *param_str( uint index )
  41. {
  42.   char *p;
  43.  
  44.   if( index >= param_count ) return NULL;
  45.   p = params;
  46.   index++;
  47.   while( --index ) p = strchr( p, 0 ) + 1;
  48.   return p;
  49. }
  50.  
  51. boolean param_opt( char *cmd )
  52. {
  53.   uint i;
  54.  
  55.   strupr( cmd );
  56.   for( i = 0; i < param_count; i++ )
  57.     if( ( strchr( param_str(i), ':' ) == NULL ) && !strncmp( param_str(i), cmd, strlen( cmd ) ) )
  58.     {
  59.       used_params >> i;
  60.       return 1;
  61.     }
  62.   return 0;
  63. }
  64.  
  65. boolean param_spec( char *cmd, char *r )
  66. {
  67.   uint i;
  68.  
  69.   strupr( cmd );
  70.   for( i = 0; i < param_count; i++ )
  71.     if( !strncmp( param_str(i), cmd, strlen( cmd ) ) )
  72.     {
  73.       strcpy(r, ¶m_str(i)[ strlen( cmd ) ] );
  74.       used_params >> i;
  75.       return 1;
  76.     }
  77.   return 0;
  78. }
  79.  
  80. boolean param_cmd( char *cmd )
  81. {
  82.   uint i;
  83.  
  84.   for( i = 0; i < param_count; i++ )
  85.     if( ( used_params & i ) && ( param_str(i)[0] != '/' ) )
  86.     {
  87.       used_params >> i;
  88.       strcpy( cmd, param_str(i) );
  89.       return 1;
  90.     }
  91.   return 0;
  92. }
  93.  
  94. boolean param_filename( char *name, char *ext )
  95. {
  96.   if( !param_cmd( name ) ) return 0;
  97.   add_ext( name, ext );
  98.   return 1;
  99. }
  100.  
  101. boolean param_filenames( char *name, char *ext )
  102. {
  103.   static char wildcard = 0;
  104.   static struct find_t ffblk;
  105.   static char drive[_MAX_DRIVE];
  106.   static char dir[_MAX_DIR];
  107.   char s[80];
  108.  
  109.   if( wildcard )
  110.   {
  111.     wildcard = !_dos_findnext( &ffblk );
  112.     if( wildcard )
  113.     {
  114.       strcpy( name, drive );
  115.       strcat( name, dir );
  116.       strcat( name, ffblk.name );
  117.       add_ext( name, ext );
  118.       return 1;
  119.     }
  120.   }
  121.   if( !wildcard && param_cmd( s ) )
  122.   {
  123.     if( ( strchr( s, '*' ) != NULL ) || ( strchr( s, '?' ) != NULL ) )
  124.     {
  125.       wildcard = !_dos_findfirst( s, _A_NORMAL, &ffblk );
  126.       if( wildcard )
  127.       {
  128.         _splitpath( s, drive, dir, NULL, NULL );
  129.         strcpy( name, drive );
  130.         strcat( name, dir );
  131.         strcat( name, ffblk.name );
  132.         add_ext( name, ext );
  133.         return 1;
  134.       }
  135.     }
  136.     else
  137.     {
  138.       add_ext( strcpy( name, s ), ext );
  139.       return 1;
  140.     }
  141.   }
  142.   return 0;
  143. }
  144.  
  145. static char *(*cmd_line)[1];
  146. static int arg_index;
  147. static char *cur_ptr;
  148. static uint cmd_count;
  149. static int handle = 0;
  150.  
  151. static char get_cmd_byte( void )
  152. {
  153.   char name[80], buf;
  154.   uint j;
  155.   unsigned num_read;
  156.  
  157.   buf = 0;
  158.   loop:
  159.     if( handle )
  160.     {
  161.       if( _dos_read( handle, &buf, 1, &num_read ) )
  162.       {
  163. #ifdef CYR
  164.         printf( "â░Ñ╕¬á: ìÑ│▒»Ñ╕¡« »░«╖Ñ▓Ñ¡ ┤á⌐½ ▒ ¬«¼á¡ñ¡¿ »á░á¼Ñ▓░¿." );
  165. #else
  166.         printf( "Error: Unable to read command line options file." );
  167. #endif
  168.         exit( ecREAD_PARAMS );
  169.       }
  170.       if( num_read )
  171.       {
  172.         if( buf < ' ' ) buf = ' ';
  173.         return toupper( buf );
  174.       }
  175.       else
  176.       {
  177.         _dos_close( handle );
  178.         handle = 0;
  179.       }
  180.     }
  181.     else
  182.     {
  183.       buf = *cur_ptr++;
  184.       if( !buf )
  185.       {
  186.         if( ++arg_index >= cmd_count ) return 0;
  187.         cur_ptr = *cmd_line[arg_index];
  188.         return ' ';
  189.       }
  190.       if( buf == '@' )
  191.       {
  192.         j = 0;
  193.         while( ( *cur_ptr != ' ' ) && ( *cur_ptr != '/' ) && ( *cur_ptr != 0 ) )
  194.           name[j++] = *(cur_ptr++);
  195.         name[j] = 0;
  196.         if( _dos_open( name, O_RDONLY, &handle) )
  197.         {
  198. #ifdef CYR
  199.           printf( "â░Ñ╕¬á: ìÑ│▒»Ñ╕¡« »░«╖Ñ▓Ñ¡ ┤á⌐½ ▒ ¬«¼á¡ñ¡¿ »á░á¼Ñ▓░¿." );
  200. #else
  201.           printf( "Error: Unable to read command line options file." );
  202. #endif
  203.           exit( ecREAD_PARAMS );
  204.         }
  205.       }
  206.       else
  207.         if( buf ) return toupper( buf );
  208.     }
  209.   goto loop;
  210. }
  211.  
  212. void __tini_comlin( void )
  213. {
  214.   uint i;
  215.  
  216.   if( !used_params.is_empty() )
  217.   {
  218.     restore_dos_screen();
  219. #ifdef CYR
  220.     printf( "â░Ñ╕¬á: ìÑ»«º¡á▓¿ «»╢¿¿ ó ¬«¼á¡ñ¡¿┐ ░Ññ: " );
  221. #else
  222.     printf( "Error: Unknown command line parameter(s):" );
  223. #endif
  224.     for( i = 0; i < param_count; i++ )
  225.       if( used_params & i )
  226.         printf( " %s", param_str( i ) );
  227.     printf( "\n" );
  228.     exit( ecUNKNOWN_PARAMS );
  229.   }
  230.   FREE( params );
  231.   param_count = 0;
  232. }
  233.  
  234. void __init_comlin( int argc, char *argv[] )
  235. {
  236.   char *p, b;
  237.   uint i;
  238.  
  239.   cmd_count = argc; cmd_line = (char *(*)[1]) argv;
  240.   params = (char *) MALLOC( PARAMS_BUFFER );
  241.   p = params;
  242.   arg_index = 0; cur_ptr = argv[arg_index];
  243.   param_count = cmd_count > 0;
  244.   while( ( b = get_cmd_byte() ) != 0 )
  245.   {
  246.     if( ( b == '/' ) || ( b == ' ') )
  247.     {
  248.       while( b == ' ' ) b = get_cmd_byte();
  249.       if( !b ) break;
  250.       if( p != params )
  251.       {
  252.         *(p++) = 0;
  253.         param_count++;
  254.       }
  255.     }
  256.     *(p++) = b;
  257.   }
  258.   *p = 0;
  259.   if( handle ) _dos_close( handle );
  260.   ~used_params;
  261.   for( i = 1; i < param_count; i++ )
  262.     used_params << i;
  263. }
  264.  
  265. #endif //NOPARAM
  266.